<?php
namespace QQ;
class Poll{
    //cookie文件地址
    private $cookie_path;
    /*
     * 设置cookie 文件
     */
    function set_cookie_file_path($uid,$tnf = false)
    {
        $this->cookie_path = COOKIE_DIR.$uid.".cookie";
        if($tnf)  return;
        if(file_exists($this->cookie_path))
        {
            unlink($this->cookie_path);
        }
        if(0 != file_put_contents($this->cookie_path,''))
        {
            \QQ\Common::echo_error('生成cookie文件失败,请检查目录权限');
            exit;
        }
        chmod($this->cookie_path,'777');
    }
    /**
    * 查询是否需要验证码
    * 
    * @access public
    * @param int $uid
    * @param string $verify
    * @return array
    */
   function check_verify($uid)
   {
	   self::set_cookie_file_path($uid);
	   $ch = curl_init("https://ssl.ptlogin2.qq.com/check?uin={$uid}&appid=1003903&r=0.14233942252344134");
	   $cookie = "confirmuin=0; ptvfsession=b1235b1729e7808d5530df1dcfda2edd94aabec43bf450d8cf037510802aa1a7dbed494c66577479895c62efa3ef35ab; ptisp=cnc";
	   curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   if (preg_match("/ptui_checkVC\('(.*)','(.*)','(.*)',[\S\s]'(.*)'\);/", $data, $verify))
	   {
	           return array_slice($verify, 1);
	   }
   }
    /**
    * 获取验证码
    * 
    * @access public
    * @param int $uid
    * @param string $verify
    * @return array
    */
   function check_verify_getimage($uid,$file = true)
   {
	   $ch = curl_init("https://ssl.captcha.qq.com/getimage?aid=1003903&r=0.2950731460005045&uin={$uid}");
	   //curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	   // curl_setopt($ch, CURLOPT_HEADER, TRUE);
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_path);
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//禁止直接显示获取的内容 重要
           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
	   $data = curl_exec($ch);
	   curl_close($ch);
           if(true === $file)
           {
                file_exists(VIRIFY_DIR.$uid.'.png') and @unlink(VIRIFY_DIR.$uid.'.png');
                if(file_put_contents(VIRIFY_DIR.$uid.'.png',$data))
                {
                    \QQ\Common::echos('生成验证码文件成功');
                }
                else
                {
                    \QQ\Common::echo_error('生成验证码文件失败');
                }
           }
           else
           {
               return $data;
           }
   }
   /**
    * 登录
    * 
    * @access public
    * @param int $uid
    * @param string $passwd
    * @param string $verify
    * @return array
    */
   function login($uid, $passwd, $verify)
   {
	   $url = "http://ptlogin2.qq.com/login?u={$uid}&p={$passwd}&verifycode={$verify}&webqq_type=10&remember_uin=1&login2qq=0&aid=1003903&u1=http%3A%2F%2Fweb2.qq.com%2Floginproxy.html%3Flogin2qq%3D0%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=3-14-18166&mibao_css=m_webqq&t=1&g=1&js_type=0&js_ver=10052&login_sig=CPc2apXzk144eot4O*JOObJcLZf2aGy9Ktd8MgGYHkXgEO-76UhSHg9pKt1REfht";
	   $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   if (preg_match("/ptuiCB\('(.*)','(.*)','(.*)','(.*)','(.*)',\s'(.*)'\);/U", $data, $verify))
	   {
	           return array_slice($verify, 1);
	   }
   }

   function p_skey($url)
   {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_path);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
   }
   
   /**
    * 真正的登录(上线)
    * 
    * @access public
    * @param string $ptwebqq
    * @return string
    */
   function login2($ptwebqq,$clientid)
   {
	   $url = "http://d.web2.qq.com/channel/login2";
	   $post_data = array();
	   $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_POSTFIELDS, "r=%7B%22status%22%3A%22online%22%2C%22ptwebqq%22%3A%22{$ptwebqq}%22%2C%22passwd_sig%22%3A%22%22%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3Anull%7D&clientid={$clientid}&psessionid=null");
	   // 必须要来路域名
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }

   /**
    * 退出
    * 
    * @access public
    * @param int $clientid
    * @param string $psessionid
    */
   function logout($clientid, $psessionid)
   {
	   $url = "http://d.web2.qq.com/channel/logout2?ids=&clientid={$clientid}&psessionid={$psessionid}&t=136587427856";
	   $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie");
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }

   /**
    * 获取消息
    * 
    * @access public
    * @param string $psessionid
    * @param int $clientid
    * @return string
    */
   function poll($psessionid,$clientid)
   {
	   $post = "r=%7B%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%2C%22key%22%3A0%2C%22ids%22%3A%5B%5D%7D&clientid={$clientid}&psessionid={$psessionid}";
	   $ch = curl_init("http://d.web2.qq.com/channel/poll2");
	   // 必须要来路域名
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }

   /**
    * 获取好友列表
    * 
    * @access public
    * @param string $vfwebqq
    * @return string
    */
   function get_user_friend($vfwebqq,$qq,$ptwebqq)
   {
	   $hash = \QQ\Poll::get_user_friend_hash($qq, $ptwebqq);
	   $post = "r=%7B%22h%22%3A%22hello%22%2C%22hash%22%3A%22{$hash}%22%2C%22vfwebqq%22%3A%22{$vfwebqq}%22%7D";
	   $ch = curl_init("http://s.web2.qq.com/api/get_user_friends2");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;

   }
   /*
    * 获得好友里需要传递的hash
    * @param qq qq号码
    * @param ptwebqq  ptwebqq的值
    * return string
    */
   public function get_user_friend_hash($qq,$ptwebqq)
   {
	   $ip = get_instance()->config->item('h_ip','node');
	   $port = get_instance()->config->item('h_port','node');
	   $ch = curl_init("http://$ip:$port/?qq={$qq}&ptwebqq={$ptwebqq}");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
      /**
        * 同意对方添加自己 并且添加对方为好友
        * 
        * @access public
        * @param int account 对方qq
        * @param int gid 组id
        * @param mname 备注名称
        * @param string vfwebqq
        * @return string
        */
   function allow_and_add($account,$vfwebqq,$gid = 0, $mname = '')
   {
           $url = 'http://s.web2.qq.com/api/allow_and_add2';
           $post = self::escape("r:{\"account\":{$account},\"gid\":{$gid},\"mname\":\"{$mname}\",\"vfwebqq\":\"{$vfwebqq}\"}");
           $ch = curl_init("http://d.web2.qq.com/channel/send_qun_msg2");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
   /*
    * 接受好友请求
    */
   function allow_added_request($account,$vfwebqq)
   {

           $url = 'http://s.web2.qq.com/api/allow_added_request2';
           $post = self::escape("r:{\"account\":{$account},\"vfwebqq\":\"{$vfwebqq}\"}");
           $ch = curl_init("http://d.web2.qq.com/channel/send_qun_msg2");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
   /*
    * 拒绝添加好友
    */
   function deny_added_request($account,$msg = '',$vfwebqq)
   {

           $url = 'http://s.web2.qq.com/api/deny_added_request2';
           $post = self::escape("r:{\"account\":{$account},\"msg\":{$msg},\"vfwebqq\":\"{$vfwebqq}\"}");
           $ch = curl_init("http://d.web2.qq.com/channel/send_qun_msg2");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
   /**
    * 获取群列表
    * 
    * @access public
    * @param string $vfwebqq
    * @return string
    */
   function get_group_name_list_mask($vfwebqq,$qq,$ptwebqq)
   {
           $hash = \QQ\Poll::get_user_friend_hash($qq, $ptwebqq);
	   $post = "r=%7B%22hash%22%3A%22{$hash}%22%2C%22vfwebqq%22%3A%22{$vfwebqq}%22%7D";
	   $ch = curl_init("http://s.web2.qq.com/api/get_group_name_list_mask2");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }

   /**
    * 获取群人员信息
    * 
    * @access public
    * @param int $gcode
    * @param string $vfwebqq
    * @return string
    */
   function get_group_info_ext($gcode, $vfwebqq)
   {
	   $url = "http://s.web2.qq.com/api/get_group_info_ext2?gcode={$gcode}&cb=undefined&vfwebqq={$vfwebqq}&t=1383984991665";
	   $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_REFERER, "http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   ob_start();
	   curl_exec($ch);
	   return ob_get_clean();
   }

   /**
    * 发送消息
    * 
    * @access public
    * @param int $from_uin
    * @param string $msg
    * @param string $psessionid
    * @param int $clientid
    * @return string
    */
   function send_buddy_msg($from_uin, $msg, $psessionid, $clientid)
   {
	    static $msg_id=79530003;
		$msg = urlencode($msg);
		$post = "r=%7B%22to%22%3A{$from_uin}%2C%22face%22%3A558%2C%22content%22%3A%22%5B%5C%22{$msg}%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A{$msg_id}%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%7D&clientid={$clientid}&psessionid={$psessionid}";

		$ch = curl_init("http://d.web2.qq.com/channel/send_buddy_msg2");
		// 必须要来路域名
		curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		$data = curl_exec($ch);
		curl_close($ch);
		$msg_id++;
		return $data;
   }

   /**
    * 发送群消息
    * 
    * @access public
    * @param int $group_id
    * @param string $msg
    * @param string $psessionid
    * @param int $clientid
    * @return string
    */
   function send_qun_msg($group_id, $msg, $psessionid, $clientid)
   {
	   static $msg_id = 77860003;
	   $msg_id++;
	   $msg = urlencode($msg);
	   $post = "r=%7B%22group_uin%22%3A{$group_id}%2C%22content%22%3A%22%5B%5C%22{$msg}%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A{$msg_id}%2C%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%7D&clientid={$clientid}&psessionid={$psessionid}";
	   $ch = curl_init("http://d.web2.qq.com/channel/send_qun_msg2");
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }

   /**
    * 获取指定用户uin
    * 
    * @access public
    * @param array $friends
    * @return int
    */
   function get_friend_uin($friends)
   {
	   foreach ($friends['result']['marknames'] as $k=>$v)
	   {
	           if ($v->markname == "!@#$%^aaD4E")
	           {
	   	      return $v->uin;
	           }
	   }
   }
   /*
    * 获得用户qq信息 不包括qq号 qq等级  qq使用时间  
    */
   function get_friend_data_qq($tuin,$vfwebqq)
   {
	    $url = "http://s.web2.qq.com/api/get_qq_level2?tuin={$tuin}&vfwebqq={$vfwebqq}&t=".time().rand(10,99);
	    $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
   /*
    * 获得用户qq信息 详细信息 比如 生肖 性别 等 个人资料里的信息  但不包括qq号码
    */
   function get_friend_information($tuin,$vfwebqq)
   {
	   $url = "http://s.web2.qq.com/api/get_friend_info2?tuin={$tuin}&verifysession=&code=&vfwebqq={$vfwebqq}&t=".time().rand(10,99);
	   $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
   /*
    * 获得qq号码
    */
   function get_friend_qq($tuin,$vfwebqq)
   {
	   
	   $url = "http://s.web2.qq.com/api/get_friend_uin2?tuin={$tuin}&verifysession=&type=1&code=&vfwebqq={$vfwebqq}&t=".time().rand(10,99);
	   $ch = curl_init($url);
	   curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
   /**
    * 获取指定群id
    * 
    * @access public
    * @param string $data
    * @return array
    */
   function get_group_gid($data)
   {
	   $r = json_decode($data);
	   $gnamelist = $r->result->gnamelist;
	   foreach ($gnamelist as $k=>$v)
	   {
	           if ($v->name == "09.经贸。电商")
	           {
	   	         return array(
	   	   	   	     "gid"=>$v->gid,
	   	   	   	     "code"=>$v->code
	   	   	       );
	           }
	   }
   }

   /**
    * 获取群里的指定用户
    * 
    * @access public
    * @param string $data
    * @return array
    */
   function get_group_user_find_uid($data)
   {
	   $r = self::obj_to_array(json_decode($data));

	   foreach ($r["result"]["minfo"] as $v)
	   {
	           if ($v['nick'] == "秋风" && $v['uin'] != USERNAME)
	           {
	   	         return $v;
	           }
	   }
   }

   /**
    * WEBQQ3.0 新版登陆加密函数
    * 
    * @access public
    * @param string $p
    * @param string $pt
    * @param string $vc
    * @param boolean $md5
    * @return string
    */
   function jspassword($pt,$p,$vc)
   {
            $ip = get_instance()->config->item('p_ip','node');
	   $port = get_instance()->config->item('p_port','node');
	   $ch = curl_init("http://$ip:$port/?pt_uin={$pt}&p={$p}&vc={$vc}");
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_path);
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	   $data = curl_exec($ch);
	   curl_close($ch);
	   return $data;
   }
  
   
   /**
    * 设置回复
    * 
    * @access public
    * @param string $key
    * @param string $value
    * @param array $replys
    * @return array
    */
   function set_reply($key, $value, &$replys)
   {
	   // key exists if replys
	   if (array_key_exists($key, $replys))
	   {
	           if ( ! is_array($replys[$key]))
	           {
	   	         $replys[$key] = (array)$replys[$key];
	           }

	           foreach ($replys[$key] as $k => $v)
	           {
	   	         if ($v == $value)
	   	         {
	   	   	       return $replys;
	   	         }
	           }

	           $replys[$key] = (array)$replys[$key];
	           array_push($replys[$key], $value);

	           return $replys;
	   }

	   // 直接保存值
	   $replys[$key] = $value;
	   return $replys;
   }

   /**
    * 删除带值回复
    * 
    * @access public
    * @param string $key
    * @param string $value
    * @param array $replys
    * @return array
    */
   function delete_reply($key, $value, &$replys)
   {
	   if ( ! array_key_exists($key, $replys))
	   {
	           return $replys;
	   }

	   if (is_array($replys[$key]) && count($replys[$key]) >= 2)
	   {
	           foreach ($replys[$key] as $k => $v)
	           {
	   	         if ($v == $value)
	   	         {
	   	   	       unset($replys[$key][$k]);
	   	   	       return $replys;
	   	         }
	           }
	   }

	   if ($replys[$key] == $value)
	   {
	           unset($replys[$key]);
	   }

	   if (is_array($replys[$key]) && implode("", $replys[$key]) == $value)
	   {
	           unset($replys[$key]);
	   }

	   return $replys;
   }

   /**
    * 删除回复
    * 
    * @access public
    * @param string $key
    * @param array $replys
    * @return array
    */
   function remove_reply($key, &$replys)
   {
	   if ( ! array_key_exists($key, $replys))
	   {
	           return $replys;
	   }

	   unset($replys[$key]);

	   return $replys;
   }

   /**
    * 读取文件
    * 
    * @access public
    * @param string $file
    * @return string
   */
   function read_file($file)
   {
	   if ( ! file_exists($file))
	   {
	           return FALSE;
	   }

	   if (function_exists('file_get_contents'))
	   {
	           return file_get_contents($file);
	   }

	   if ( ! $fp = @fopen($file, 'rb'))
	   {
	           return FALSE;
	   }

	   flock($fp, LOCK_SH);

	   $data = '';
	   if (filesize($file) > 0)
	   {
	           $data =& fread($fp, filesize($file));
	   }

	   flock($fp, LOCK_UN);
	   fclose($fp);

	   return $data;
   }

   /**
    * 写入文件
    * 
    * @access public
    * @param string $path
    * @param string $data
    * @param string $mode
    * @return boolean
    */
   function write_file($path, $data, $mode = "wb")
   {
	   if ( ! $fp = @fopen($path, $mode))
	   {
	           return FALSE;
	   }

	   flock($fp, LOCK_EX);
	   fwrite($fp, $data);
	   flock($fp, LOCK_UN);
	   fclose($fp);

	   return TRUE;
   }

   /**
    * 对象转数组
    * 
    * @access public
    * @param object $obj
    * @return array
    */
   function obj_to_array($obj)
   {
	   $ret = array();

	   foreach ($obj as $key =>$value)
	   {
	           if (gettype($value) == 'array' || gettype($value) == 'object')
	           {
	   	         $ret[$key] = self::obj_to_array($value);
	           }
	           else
	           {
	   	         $ret[$key] = $value;
	           }
	   }

	   return $ret;
   }


   /**
    * 解析cookie
    *
    * @access public
    * @return array
    */
   function parse_cookie()
   {
	   // Netscape HTTP Cookie File
	   $cookies = file($this->cookie_path);

	   $data = array();
	   foreach ($cookies as $v)
	   {
	           if (preg_match("/(.*\.qq\.com)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\n/U", $v, $p))
	           {
	   	         $data[] = array_slice($p, 1);
	           }
	   }
	   return $data;
   }

   /**
    * 获取cookie
    * 
    * public 
    * @param array $cookie
    * @return array
    */
   function get_cookie($cookie = NULL)
   {
	   if ($cookie === NULL)
	   {
	           $cookie = self::parse_cookie();
	   }

	   if (is_array($cookie) && count($cookie)<=6)
	   {
	           return FALSE;
	   }

	   foreach ($cookie as $v)
	   {
	           $data[$v[5]] =$v[6];
	   }

	   return $data;
   }
    /*
    * escape编码
    */
   function escape($str){
        $reString = '';
        preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$newstr);
        $ar = $newstr[0];
        foreach($ar as $k=>$v){
           if(ord($ar[$k])>=127){
            $tmpString=bin2hex(iconv("GBK","ucs-2//IGNORE",$v));
            if (!eregi("WIN",PHP_OS)){
             $tmpString = substr($tmpString,2,2).substr($tmpString,0,2);
            }
            $reString.="%u".$tmpString;
           }else{
            $reString.= rawurlencode($v);
           }
        }
        return $reString;
    }

    function unescape ($source){ 
        $decodedStr = ""; 
        $pos = 0; 
        $len = strlen ($source); 
        while ($pos < $len){ 
           $charAt = substr ($source, $pos, 1); 
                 if ($charAt == '%'){ 
            $pos++; 
            $charAt = substr ($source, $pos, 1); 
                     if ($charAt == 'u'){ 
             // we got a unicode character 
             $pos++; 
             $unicodeHexVal = substr ($source, $pos, 4); 
             $unicode = hexdec ($unicodeHexVal); 
             $entity = "&#". $unicode . ';'; 
             $decodedStr .= utf8_encode ($entity); 
             $pos += 4; 
                     }else{ 
             // we have an escaped ascii character 
             $hexVal = substr ($source, $pos, 2); 
             $decodedStr .= chr (hexdec ($hexVal)); 
             $pos += 2; 
            } 
           }else{ 
            $decodedStr .= $charAt; 
            $pos++; 
           } 
        } 
        return $decodedStr; 
    }
   
}


/*
 * 其他链接
 * 查看陌生人资料  如果别人添加你   你还没有接受 而想看别人的信息 那么就用这个链接
 * http://s.web2.qq.com/api/get_stranger_info2
 * ?tuin=1394545133
 * &verifysession=
 * &gid=0
 * &code=
 * &vfwebqq=f6532e27007c840b8b60646d7d1b29b6e726932b239e9b1b75cda1179437cf187e5a3405383c3d3a&t=1412687017619
 * 
 * 同意添加好友 并且加对方为好友
 * http://s.web2.qq.com/api/allow_and_add2  
 * post数据  r:{"account":qq,"gid":组id[0],"mname":"备注","vfwebqq":"vfwebqq值"}
 * 
 * 同意添加
 * http://s.web2.qq.com/api/allow_added_request2
 * post值  r:{"account":397109515,"vfwebqq":"f6532e27007c840b8b60646d7d1b29b6e726932b239e9b1b75cda1179437cf187e5a3405383c3d3a"}
 * 
 * 拒绝添加
 * http://s.web2.qq.com/api/deny_added_request2
 * post值  r:{"account":397109515,"msg":"","vfwebqq":"f6532e27007c840b8b60646d7d1b29b6e726932b239e9b1b75cda1179437cf187e5a3405383c3d3a"}
 * 
 * http://d.web2.qq.com/channel/get_offpic2?
 * file_path=%2F20e482d1-6ca1-4526-9f4b-7b8159d4596e
 * &f_uin=1394545133
 * &clientid=88745466
 * &psessionid=psessionid
 * 
 * 下载图片 由上面的链接 302过来
 * http://202.55.10.73/
 * ?ver=2173
 * &rkey=952480dfe8a92edbaf297e91c7e98fe862be5cb11fb817e6b2fb7ba278f99f732f8e5591adbb74596dc7a9c31eb8ce97526e1b02bd091fd9677261c1d9bff42f
 */